import { EActionCode } from "framework/FSM/Action/ActionCode";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { ActionUtils } from "../../../State/creep/utils/ActionUtil";
import { dangrous_style } from "utils/constants/globalconstant";
import { hold_place } from "GameLoop/WorldState/mount/move/action.virtual";

/**
 *  跑路，原理危险
 */
export class ActionEscapeDanger  extends ActionBaseOfCreep<IActionCMD<"escape">>
{
    protected _execute(action: IActionCMD<"escape">): ActionCode 
    {
        // 给定一个远离的坐标集合。在这个范围内就原理
        // 因为要提前远离sourceKeeper。但是porter没有绑定source所以只能传坐标进来
        // 并且也不能单纯以hostilecreep作为判断标准
        const creep = action.arg.entity;
        const ret = ActionUtils.isFleeForSourceKeeper(creep as Creep,6);
        if(ret ===0)
        {
            creep.log(`info`,`${8}格内没有敌方单位`,);
            hold_place(creep);
            return EActionCode.OK;
        }
        let goals = _.clone(action.arg.goals);
        if(ret[1])
        {
            goals = ret[1].map(e=>{return {pos:e.pos,range:5}})
             
        }
        else if(ret[2])
        {
            goals = ret[2].map(e=>{return {pos:e.pos,range:5}})
        }
        creep.flee( goals,{IsBlockExits:true});
        goals.forEach(e=>
            {
                creep.room!.visual.line(creep.pos.x,creep.pos.y,e.pos.x,e.pos.y,dangrous_style);
                creep.room!.visual.text(`${creep.pos.distance(e.pos)}:${e.range}`,e.pos.x,e.pos.y,dangrous_style);
            })
       
    
        return EActionCode.RUNNING;
    }
}